package com.hualu.xmlCode.dao.hibernate;

import java.util.ArrayList;
import java.util.List;

import org.apache.commons.lang.StringUtils;
import org.hibernate.Criteria;
import org.hibernate.HibernateException;
import org.hibernate.Session;
import org.hibernate.criterion.DetachedCriteria;
import org.hibernate.criterion.Restrictions;
import org.springframework.orm.hibernate3.HibernateCallback;
import org.tnc.dao.hibernate.GenericDaoHibernate;

import com.hualu.xmlCode.dao.OthersAlbumViewDao;
import com.hualu.xmlCode.model.AlbumViewSearchCondition;
import com.hualu.xmlCode.model.OthersAlbumView;

public class OthersAlbumViewDaoHibernate extends GenericDaoHibernate<OthersAlbumView, String> implements OthersAlbumViewDao {

    public OthersAlbumViewDaoHibernate() {
        super(OthersAlbumView.class);
    }
    
    
    public List<OthersAlbumView> getOthersAlbumViewListByCondition(AlbumViewSearchCondition albumViewSearchCondition) {
        
        DetachedCriteria c = DetachedCriteria.forClass(OthersAlbumView.class);
        List<OthersAlbumView> list = new ArrayList<OthersAlbumView>();
        
        // 视频名称
        if (StringUtils.isNotBlank(albumViewSearchCondition.getVideoId())) {
            c.add(Restrictions.like("id", "%" + albumViewSearchCondition.getVideoId() + "%"));
        }
        
        // 视频名称
        if (StringUtils.isNotBlank(albumViewSearchCondition.getVideoName())) {
            c.add(Restrictions.like("videoName", "%" + albumViewSearchCondition.getVideoName() + "%"));
        }
        
        list = this.findByCriteria(c);
        
        return list;
    }

    
    @SuppressWarnings("unchecked")
    public List<OthersAlbumView> findByCriteria(final DetachedCriteria detachedCriteria) {
        return (List) getHibernateTemplate().execute(new HibernateCallback() {
            public Object doInHibernate(Session session) throws HibernateException {
                Criteria criteria = detachedCriteria.getExecutableCriteria(session);
                return criteria.list();
            }
        });
    }

}
